package com.service.impl;

import com.bean.Page;
import com.bean.User;
import com.dao.UserDao;
import com.dao.impl.UserDaoImpl;
import com.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {

    private UserDao userDao=new UserDaoImpl();

    @Override
    public boolean existsUserid(String userid) {
        if(userDao.existsUserid(userid)==null){
            return false;
        }
        return true;
    }
    @Override
    public User login(User user) {
        return userDao.login(user.getUserid(),user.getPassword());
    }

    @Override
    public void register(User user) {
        userDao.saveUser(user);
    }

    @Override
    public void deleteByUserid(String userid) {
        userDao.deleteByUserid(userid);
    }

    @Override
    public void modifyPass(String userid,String password) {
        userDao.updatePass(userid,password);
    }

    @Override
    public void modifyMess(String userid,String pet_name,String relname,String postal_code,String telephone,String address,int sex) {
        userDao.updateMess(userid,pet_name, relname, postal_code, telephone,address,sex);
    }

    @Override
    public void modifyPhoto(String userid, String head_sculpture) {
        userDao.updatePhoto(userid,head_sculpture);
    }

    @Override
    public Page<User> queryPageUser(int pageNo, int pageSize) {
        Page<User>page=new Page<User>();
        //设置当前页码
        page.setPageNo(pageNo);
        //设置每页显示的数量
        page.setPageSize(pageSize);

        //求总记录数
        Integer pageTotalCount=userDao.queryForPageTotalCount();
        //设置总记录数
        page.setPageTotalCount(pageTotalCount);
        //求总页码
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount%pageSize>0){
            pageTotal+=1;
        }
        //设置总页码
        page.setPageTotal(pageTotal);
        //求当前页数据开始的索引
        int begin=(page.getPageNo()-1)*pageSize;
        //求当前页数据
        List<User> items=userDao.queryForPageItem(begin,pageSize);
        //设置当前页数据
        page.setItem(items);

        return page;
    }

    @Override
    public User showMess(String enterpriseid) {
        return userDao.showMess(enterpriseid);
    }
}
